//
//  YSSearchViewController.m
//  YSKitDemo
//
//  Created by Kyson on 2018/12/19.
//  Copyright © 2018 YangShen. All rights reserved.
//

#import "YSSearchViewController.h"

@interface YSSearchViewController () <UISearchResultsUpdating>
{
    UISearchController *searchController;
    NSArray<NSString *> *titles;
    NSArray<NSString *> *filterTitles;
}

@end

@implementation YSSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"UISearchController";
    self.edgesForExtendedLayout = UIRectEdgeNone;    
    
    // push pop 回来 searchbar自动隐藏了
    self.definesPresentationContext = NO;
    
    searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    searchController.searchResultsUpdater = self;
    searchController.searchBar.keyboardType = UIKeyboardTypeNumberPad;
    // 搜索时，背景变暗色
    if (@available(iOS 9.1, *)) {
        searchController.obscuresBackgroundDuringPresentation = NO;
    } else {
        searchController.dimsBackgroundDuringPresentation = NO;
    }
    searchController.searchBar.placeholder = @"请输入";
    self.tableView.tableHeaderView = searchController.searchBar;
    // 在UISearchController在激活状态下用户push到下一个view controller之后search bar不会仍留在界面上。
    self.definesPresentationContext = YES; // 这是push成功的关键
    searchController.hidesNavigationBarDuringPresentation = YES;
    
    searchController.searchBar.frame = CGRectMake(searchController.searchBar.frame.origin.x,searchController.searchBar.frame.origin.y,searchController.searchBar.frame.size.width,44.0);
    
    NSMutableArray<NSString *> *temp = [NSMutableArray array];
    for (int i=0; i<40; i++) {
        NSString *title = [NSString stringWithFormat:@"%d-%u", i,arc4random()];
        [temp addObject:title];
    }
    titles = [NSArray arrayWithArray:temp];
}

#pragma mark - Private
- (BOOL)isFiltering {
    return searchController.isActive && ![self searchBarIsEmpty];
}

- (BOOL)searchBarIsEmpty {
    return searchController.searchBar.text.length > 0;
}

- (void)filterContentForSearchText:(NSString *)searchText {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchText];
    filterTitles = [titles filteredArrayUsingPredicate:predicate];
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self isFiltering]) {
        return filterTitles.count;
    }
    return titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * const identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    if ([self isFiltering]) {
        cell.textLabel.text = filterTitles[indexPath.row];
    } else {
        cell.textLabel.text = titles[indexPath.row];
    }
    
    return cell;
}

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    
    [self filterContentForSearchText:searchController.searchBar.text];    
}

- (void)dealloc {
    NSLog(@"%@ - [dealloc]", NSStringFromClass(self.class));
}

@end
